package com.wangle.server.hot100;

import com.wangle.server.util.ListNode;
import org.springframework.stereotype.Service;

@Service
public class ListNodeSum {
    // 链表之和
    public ListNode addTwoNums(ListNode node1, ListNode node2) {
        ListNode res = new ListNode(0);
        ListNode cur = res;
        int carry = 0;
        while (node1!=null || node2!=null) {
            int x = node1 == null ? 0 : node1.val;
            int y = node2 == null ? 0 : node2.val;
            int sum = x+y+carry;
            carry = sum/10;
            sum = sum%10;
            cur.next = new ListNode(sum);
            cur = cur.next;
            if (node1!=null) {
                node1 = node1.next;
            }
            if (node2!=null) {
                node2 = node2.next;
            }
        }
        if (carry == 1) {
            cur.next = new ListNode(carry);
        }
        return res.next;
    }
}
